package models

import (
	"encoding/json"
	"fmt"

	"github.com/beego/beego/v2/client/orm"
)

type LogComment struct {
	WriterSN   string
	WriterName string
	Content    string
	CreatTime  int64
}

func AppendLogChild(logid int, child *LogComment) (err error) {
	o := orm.NewOrm()

	var v TUserLog

	qs := o.QueryTable(v.TableName())
	qs = qs.Filter("id", logid)
	err = qs.One(&v, "children")
	if err != nil {
		return
	}

	var children []*LogComment
	if len(v.Children) > 0 {
		err = json.Unmarshal([]byte(v.Children), &children)
		if err != nil {
			return
		}
	}
	children = append(children, child)

	var childrenBytes []byte
	fmt.Println(children)
	childrenBytes, err = json.Marshal(children)
	if err != nil {
		return err
	}
	v.Children = string(childrenBytes)
	v.Id = logid
	_, err = o.Update(&v, "children")
	if err != nil {
		return err
	}

	return
}
